<template>
  <div class="page">
    <div class="searchline" style="margin: 20px 0">
      <el-input
        placeholder="用户名"
        v-model="search.name"
        size="medium"
        style="width: 200px; margin-left: 12px"
      ></el-input>
      <el-input
        placeholder="账号"
        v-model="search.phone"
        size="medium"
        style="width: 200px; margin-left: 12px"
      ></el-input>
      <el-select
        placeholder="状态"
        size="medium"
        v-model="search.status"
        style="width: 200px; margin-left: 12px"
      >
        <el-option label="正常" :value="0"> </el-option>
        <el-option label="停用" :value="1"> </el-option>
      </el-select>
      <el-select
        placeholder="角色"
        size="medium"
        v-model="search.role"
        style="width: 200px; margin-left: 12px"
      >
        <el-option label="学生" :value="0"> </el-option>
        <el-option label="企业" :value="1"> </el-option>
        <el-option label="管理员" :value="2"> </el-option>
      </el-select>
      <el-button
        style="margin-left: 12px"
        size="medium"
        type="primary"
        @click="searchFromList(search)"
        >筛选</el-button
      >
      <el-button size="medium" type="info" @click="reSet()">重置</el-button>
    </div>
    <el-table
      header-row-class-name="tableHeader"
      :data="tableData"
      border
      style="width: 98%; margin-left: 12px"
    >
      <el-table-column prop="name" label="用户名" width="150px">
      </el-table-column>
      <el-table-column prop="phone" label="账号"> </el-table-column>
      <el-table-column prop="role" label="角色" width="80px">
        <template slot-scope="scope">
          <span>{{
            scope.row.role == 0
              ? "学生"
              : scope.row.role == 1
              ? "企业"
              : "管理员"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80px">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1" style="color: red">停用</span>
          <span v-if="scope.row.status == 0" style="color: rgb(65, 151, 46)"
            >正常</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="created_date" label="创建日期"> </el-table-column>
      <el-table-column prop="updated_date" label="更新日期" width="150px">
      </el-table-column>
      <el-table-column prop="empty" label="操作" width="180px">
        <template slot-scope="scope">
          <span v-if="scope.row.role !== 2">
            <el-button
              v-if="scope.row.status == 1"
              size="mini"
              type="primary"
              @click="setUserStatus(scope.row.id, 0)"
              >恢复</el-button
            >
            <el-button
              v-if="scope.row.status == 0"
              size="mini"
              type="info"
              @click="setUserStatus(scope.row.id, 1)"
              >停用</el-button
            >
            <el-button
              size="mini"
              type="primary"
              @click="setPassword(scope.row.id)"
              >密码重置</el-button
            >
          </span>
          <span v-else>无法操作管理员 </span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { AllUser } from "@/api/user";
import { localData } from "@/util/local";
import { dateFormat } from "@/util/common";
import { userChange, resetPassword } from "@/api/user";

export default {
  data() {
    return {
      tableData: [],
      initTableData: [],
      userInfo: {},
      search: {
        name: "",
        status: "",
        phone: "",
        role: "",
      },
    };
  },
  created() {
    this.userInfo = localData("get", "userinfo");
  },
  mounted() {
    this.getAllUser();
  },
  methods: {
    getAllUser() {
      AllUser().then((res) => {
        this.initTableData = res.data.users.map((item) => {
          item.created_date = dateFormat(
            "YYYY-mm-dd HH:MM",
            new Date(item.created_date)
          );
          item.updated_date = dateFormat(
            "YYYY-mm-dd HH:MM",
            new Date(item.updated_date)
          );
          return item;
        });
        this.tableData = this.initTableData;
        this.searchFromList(this.search);
      });
    },
    setUserStatus(id, status) {
      const param = {
        id: id,
        status: status,
      };
      userChange(param)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: status === 1 ? "账号停用成功" : "账号已恢复",
              type: "success",
            });
            this.getAllUser();
          }
        })
        .catch((err) => {
          this.$message({
            showClose: true,
            message: err.data,
            type: "warning",
          });
        });
    },
    setPassword(id) {
      const param = {
        id: id,
        password: "123456",
      };
      resetPassword(param)
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "账号密码已重置为123456",
              type: "success",
            });
          }
        })
        .catch((err) => {
          this.$message({
            showClose: true,
            message: err.data,
            type: "warning",
          });
        });
    },
    searchFromList(search) {
      const { name, status, phone, role } = search;
      this.tableData = this.initTableData.filter((item) => {
        return (
          item.name.toString().indexOf(name.toString()) !== -1 &&
          item.status.toString().indexOf(status.toString()) !== -1 &&
          item.phone.toString().indexOf(phone.toString()) !== -1 &&
          item.role.toString().indexOf(role.toString()) !== -1
        );
      });
    },
    reSet() {
      this.search = {
        name: "",
        status: "",
        phone: "",
        role: "",
      };
      this.tableData = this.initTableData;
    },
  },
};
</script>

<style>
.tableHeader {
  color: rgb(65, 151, 46) !important;
}
</style>